import { defineStore } from "pinia";
import { ref } from "vue";
import { getMenulistApi } from "@/api/permission";
import recursionRoutes from "@/utils/recursionRoutes ";
import allRoutes from '@/router/dynamicRoutes.js'
import commonRoute from "../router/commonRoute"; 
import router from "../router/index";
export default defineStore("menulist", () => {
  let menulist = ref([]);

  let getMenulist = ()=>new Promise(async (resolve, reject) => {
    let result = await getMenulistApi()
    // console.log(result);
    //  menulist.value = result.data.menulist
   let userRoutes =  recursionRoutes(allRoutes,result.data.menuList)
      //  console.log(userRoutes);
     commonRoute.children = userRoutes
     //将路由动态添加到路由配置中
     //核心代码addRoute
     router.addRoute(commonRoute)
     menulist.value = userRoutes
    //  console.log(menulist.value);
     resolve()
  })
  return { menulist, getMenulist };
})

